<?php 
namespace Api\Controller;
class TermController extends CommonController{
	
	protected $m;

	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclTerm');
	}
	
	//学期id
	public function get_term_by_id($id){
		return $this->m->where(array('id'=>$id))->find();
	}
	
	//显示最近的20条学期
	public function get_term_infos_for_limit(){
		return $infos = $this->m->order('listorder desc,id desc')->limit(20)->select();
	}
	
	//学期信息-中途报名包含本学期
	public function get_term_infos_for_midbm(){
		$now = Date('Y-m-d',time());
		$map = array(
				'status'=>1,
				'end_time'=>array('egt',$now),
				'id'=>array('not in','17,18')
		);
		return $infos = $this->m->where($map)->order('studyyear_count desc,listorder desc,id desc')->select();
	}
	
	//学期信息-暑期班中途报名包含本学期
	public function get_term_infos_for_summer(){
		$now = Date('Y-m-d',time());
		$map = array(
				'status'=>1,
				'pay_end_time'=>array('egt',$now),
				'id'=>array('in','17,18')
		);
		return $infos = $this->m->where($map)->order('studyyear_count desc,listorder desc,id desc')->select();
	}
	
	//未过期的学期信息
	public function get_term_infos_wgq(){
		$now = Date('Y-m-d',time());
		$map = array(
				'status'=>1,
				'end_time'=>array('egt',$now)
		);
		return $infos = $this->m->where($map)->order('studyyear_count desc,listorder desc,id desc')->select();
	}
	
	//学期信息
	public function get_term_infos(){
		$now = Date('Y-m-d',time());
		$map = array(
			'status'=>1,
			'pay_end_time'=>array('egt',$now),
			'id'=>array('not in','17,18')
		);
		return $infos = $this->m->where($map)->order('studyyear_count desc,listorder desc,id desc')->select();
	}
	
	
	public function check_term_pay(){
		$term = $this->get_term_info();
		if($term){
			$starttime = strtotime($term['pay_start_time']);
			$endtime = strtotime($term['pay_end_time']);
			if($starttime <= time() && $endtime >= time()){
				return $term;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	public function get_pre_next_xuexian_by_tid($tid){
		$studyyear = $this->m->where(array('id'=>$tid))->getField('studyyear');
		$studyyear_arr = explode(',',$studyyear);
		foreach ($studyyear_arr as $sy){
			$xnids[] = R('Api/Termtime/get_xnid',array($sy));
		}
		$m = M('YgclTermTime');
		$info = $m->where(array('id'=>array('in',$xnids)))->order('start_time asc')->select();
		$next_xuenian = $info[0];
		$pre_info = $m->where(array('start_time'=>array('LT',$next_xuenian['start_time'])))->order('start_time desc')->find(); 
		return array('pre_xnid'=>$pre_info['id'],'next_xnid'=>$next_xuenian['id']);
	}
	
}